package com.example.demomail.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demomail.model.MailContentVo;
import com.example.demomail.model.RiskAlarmInfoVo;
import com.example.demomail.service.MailSendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author 赵恒
 * @date 2023/6/8
 */
@RestController
@RequestMapping("/mail")
@Api(tags = "发送邮件")
@Slf4j
public class MailSendController {
    @Resource
    private MailSendService mailSendService;


    @ApiOperation("邮件内容回显")
    @PostMapping("/echoMailContent")
    public MailContentVo getMailContent(@RequestBody RiskAlarmInfoVo riskAlarmInfoVo) {
        log.info("请求参数：{}", JSONObject.toJSONString(riskAlarmInfoVo));
        return mailSendService.getMailContent(riskAlarmInfoVo);

    }

    @ApiOperation("发送邮件")
    @PostMapping("/sendMail")
    public String sendMail(@RequestBody MailContentVo mailContentVo) {
        log.info("请求参数：{}", JSONObject.toJSONString(mailContentVo));
        boolean b = mailSendService.sendMail(mailContentVo);
        if (b) {
            // 保存发送记录到数据库中
            log.info("邮件发送成功已保存到数据库中");
            return "发送成功";
        } else {
            return "发送失败";
        }
    }

    @ApiOperation(("发本地附件"))
    @PostMapping("/localAttach")
    public String sendMailAndAttach(@RequestBody MailContentVo mailContentVo) {
        log.info("请求参数：{}", JSONObject.toJSONString(mailContentVo));
        return mailSendService.sendMailAndAttach(mailContentVo);
    }

    @ApiOperation("发邮件和上传多个附件")
    @PostMapping("/uploadAttach")
    public String sendMailAndAttachWeb(@RequestParam(value = "files") MultipartFile[] files,
                                       @RequestParam(value = "mailContentVoStr")String mailContentVoStr) {
       return mailSendService.sendMailAndAttachWeb(files, mailContentVoStr);
    }
}
